package com.devicemantra.struts.view.result;

import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Result;

public class JSONResult implements Result {

	private static final long serialVersionUID = 1L;

	public void execute(final ActionInvocation invocation) throws Exception {
		ServletActionContext.getResponse().setContentType("application/json");
		final Gson gson = new GsonBuilder()
				.excludeFieldsWithoutExposeAnnotation().create();
		final Object action = invocation.getAction();
		final String jsonString = gson.toJson(action);
		System.out.println("JSON Representation " + jsonString);
		ServletActionContext.getResponse().getWriter().write(jsonString);
	}
}
